<?php

/**
 * review actions.
 *
 * @package    lezgro
 * @subpackage review
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class reviewActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      $solution_id=$request->getParameter('solution_id');
    if($solution_id!=null&&$solution_id!=''){
    $this->reviews = Doctrine_Core::getTable('Review')->getReviewsBySolutionId($solution_id);
    $this->solution_id=$solution_id;
    $this->solution=Doctrine::getTable('Solution')->findOneById($solution_id);
    }else{
    $this->reviews = Doctrine_Core::getTable('Review')->getReviews();
    }
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ReviewForm();
    $this->form->setDefault('solution_id', $request->getParameter('solution_id'));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ReviewForm();

    $review=$this->processForm($request, $this->form);
     $forma=($request->getParameter($this->form->getName()));
       $solution_id=$forma['solution_id'];
       $solutionReview=new SolutionReview();
       $solutionReview->setSolutionId($solution_id);
       $solutionReview->setReviewId($review->getId());
       $solutionReview->save();
       $this->redirect('review/edit?id='.$review->getId());
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($review = Doctrine_Core::getTable('Review')->find(array($request->getParameter('id'))), sprintf('Object review does not exist (%s).', $request->getParameter('id')));
    $this->form = new ReviewForm($review);
    $this->review=$review;
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($review = Doctrine_Core::getTable('Review')->find(array($request->getParameter('id'))), sprintf('Object review does not exist (%s).', $request->getParameter('id')));
    $this->form = new ReviewForm($review);

    $this->review=$this->processForm($request, $this->form);
    if(isset($this->review))
        $this->redirect( array('module'=>'review', 'action'=>'index','solution_id'=>$review->getSolution()->getId()));

    $this->setTemplate('edit');
  }
  public function executeCreateMainReview(sfWebRequest $request){
      $solution_id=$request->getParameter('solution_id');
    if($solution_id!=null&&$solution_id!=''){
    $solution=Doctrine::getTable('Solution')->findOneById($solution_id);}
    $mainReview=$solution->getMainReview();
    if($mainReview->getText()){
    }else{
        $mainReview=new Review();
        $mainReview->save();
        $solution->setMainReview($mainReview);
        $solution->save();
    }
    //echo $mainReview->getMainSolution()->getId();die();
    $this->form=new MainReviewForm ($mainReview);
  }
 public function executeCreateMain(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new MainReviewForm();

    $review=$this->processForm($request, $this->form);
     $forma=($request->getParameter($this->form->getName()));
       $this->redirect('review/edit?id='.$review->getId());
    $this->setTemplate('new');
  }
  public function executeUpdateMain(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($review = Doctrine_Core::getTable('Review')->find(array($request->getParameter('id'))), sprintf('Object review does not exist (%s).', $request->getParameter('id')));
    $this->form = new MainReviewForm($review);

    $this->review=$this->processForm($request, $this->form);
    if(isset($this->review))
        $this->redirect( array('module'=>'solution', 'action'=>'index'));

    $this->setTemplate('edit');
  }
  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($review = Doctrine_Core::getTable('Review')->find(array($request->getParameter('id'))), sprintf('Object review does not exist (%s).', $request->getParameter('id')));
    
    $review->delete();

    $this->redirect('review/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $review = $form->save();

      return $review;
    }
  }
}
